package com.example.service.impl;

import com.example.domian.Emp;
import com.example.mapper.EmpMapper;
import com.example.page.EmpPageQuery;
import com.example.page.PageBean;
import com.example.service.EmpService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

//操作Emp的业务层接口实现类
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    //修改员工
    @Override
    public void update(Emp emp) {
        LocalDateTime now = LocalDateTime.now();
        emp.setUpdateTime(now);
        empMapper.update(emp);
    }

    //根据id删除员工
    @Override
    public void deleteById(Integer id) {
        empMapper.delete(id);
    }

    //根据id查询员工
    @Override
    public Emp selectById(Integer id) {
        return empMapper.selectById(id);
    }

    //添加员工
    @Override
    public void insert(Emp emp) {
        LocalDateTime now = LocalDateTime.now();
        emp.setCreateTime(now);
        emp.setUpdateTime(now);
        empMapper.insert(emp);
    }

    //手动分页，模糊查询
    @Override
    public PageBean<Emp> findListByCondition(EmpPageQuery empPageQuery) {

        //逻辑判断，是否传入当前页码pageNum与每页多少条pageSize
        if (empPageQuery.getPageNum()==null){
            empPageQuery.setPageNum(1);
        }
        if (empPageQuery.getPageSize()==null){
            empPageQuery.setPageSize(5);
        }

        //创建返回Bean
        PageBean<Emp> pageBean = new PageBean<>();
        pageBean.setPageSize(empPageQuery.getPageSize());
        pageBean.setPageNum(empPageQuery.getPageNum());

        //计算总记录数
        Long totalCount = empMapper.findCountByCondition(empPageQuery);
        pageBean.setTotalCount(totalCount);

        //计算总页数，即总记录数/每页记录数（向上取整）
        int totalPage = (int) Math.ceil(totalCount / pageBean.getPageSize());
        pageBean.setTotalPage(totalPage);

        //获取当前页数据，由于sql利用的index起始索引与pageSize每页显示多少条，的条件
        empPageQuery.setStartIndex((empPageQuery.getPageNum()-1)*empPageQuery.getPageSize());
        List<Emp> dataList = empMapper.findListByCondition(empPageQuery);
        pageBean.setDataList(dataList);

        return pageBean;
    }

    //根据pageHelper插件，分页查询
    @Override
    public PageBean<Emp> findPageByCondition(EmpPageQuery empPageQuery) {
        //逻辑判断，是否传入当前页码pageNum与每页多少条pageSize
        if (empPageQuery.getPageNum()==null){
            empPageQuery.setPageNum(1);
        }
        if (empPageQuery.getPageSize()==null){
            empPageQuery.setPageSize(5);
        }

        //设置分页条件
        PageHelper.startPage(empPageQuery.getPageNum(),empPageQuery.getPageSize());
        //调用查询所有
        List<Emp> dataList = empMapper.findPageByCondition(empPageQuery);
        //封装返回值
        PageInfo pageInfo = new PageInfo(dataList);
        PageBean<Emp> pageBean = new PageBean<>();
        pageBean.setPageNum(empPageQuery.getPageNum());
        pageBean.setPageSize(empPageQuery.getPageSize());
        pageBean.setTotalPage(pageInfo.getPages());
        pageBean.setTotalCount(pageInfo.getTotal());
        pageBean.setDataList(dataList);

        return pageBean;
    }

    @Override
    public List<Emp> findByDeptId(Integer deptId) {
        List<Emp> empList = empMapper.findByDeptId(deptId);
        return empList;
    }


}
